
define(['ionic', 'controllers'], function () {
    angular.module('U9Mobile.controllers')
        .controller('TempCtrl', ['$scope', '$state','$stateParams','AppManager', 'UserService', 'UtilService',
            function ($scope, $state,$stateParams, AppManager, UserService, UtilService) {

                $scope.data = {
                    url: $stateParams.param? AppManager.getActiveAppUrl()+$stateParams.param:AppManager.getActiveAppUrl()
                };
                if ($scope.data.url) {
                    u9.showLoading();
                    var appIFrame = document.getElementById('appIFrame');
                    if (appIFrame.attachEvent) {
                        appIFrame.attachEvent("onload", onIFrameReady);
                    } else {
                        appIFrame.onload = onIFrameReady;
                    }
                }

                $scope.goHome = function () {
                    var appId = AppManager.getActiveAppId();
                    if (window.Analytics) {
                        window.Analytics.analytics("goHome", AppManager.getActiveAppName(appId));
                    }
                    AppManager.setActiveAppId('');

                    UtilService.goState('app.home');
                };
                $scope.goChat = function () {
                    $state.go('app.chat', {
                        fromApp: true
                    });
                };
                $scope.goAbout = function () {
                    AppManager.setActiveAppId('');
                    UtilService.goState('app.about');
                };

                function updateStylesheet() {
                    if (navigator.userAgent.toLowerCase().match(/android/i) === 'android') {
                        return;
                    }
                    var style = document.createElement('style');
                    style.appendChild(document.createTextNode(""));
                    window.frames['appIFrame'].document.head.appendChild(style);
                    style.sheet.insertRule(".pane,.bar {width:" + window.innerWidth + "px !important;}", 0);
                    style.sheet.insertRule("@media all and (orientation:landscape) { .pane,.bar { width:" + window.innerHeight + "px;}}", 1);
                }
                function onIFrameReady () {
                    updateStylesheet();
                }
            }
        ]);
});